//
//  ELBlankLogInView.swift
//  SinaWeiBo
//
//  Created by jiwang on 15/12/15.
//  Copyright © 2015年 jiwang. All rights reserved.
//

import UIKit

protocol ELBlankLogInViewDelegate : NSObjectProtocol {
    
    ///注册点击事件
    func registeClick ()
    ///登录点击事件
    func loginClick ()
    
}

class ELBlankLogInView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupSubViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    weak var delegate : ELBlankLogInViewDelegate?
    
    ///设置图片和文字
    func setIconViewAndLabelWith (imageName : String,titleName : String) {
        homeView.image = UIImage(named: imageName)
        desLabel.text = titleName
        desLabel.sizeToFit()
        
        //隐藏圆圈图
        iconView.hidden = true
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    
    private  func setupSubViews() {
    
        addSubview(iconView)
        addSubview(homeView)
        addSubview(desLabel)
        addSubview(registerButton)
        addSubview(logInButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        logInButton.translatesAutoresizingMaskIntoConstraints = false
        desLabel.translatesAutoresizingMaskIntoConstraints = false
        
        
        //设置iconView的约束
        //CenterX
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self , attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //CenterY 中心对齐
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //设置HomeView的约束 ===================
        //中心对齐
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //设置laber 的中心对齐约束 ===================
        self.addConstraint(NSLayoutConstraint(item: desLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //距离圆圈16
        self.addConstraint(NSLayoutConstraint(item: desLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: desLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        //设置注册按钮 ===================
        //左对齐
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: desLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        //距离laber底部16
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: desLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽100
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高35
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置登录按钮 ===================
        //左对齐
        self.addConstraint(NSLayoutConstraint(item: logInButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: desLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        //距离laber底部16
        self.addConstraint(NSLayoutConstraint(item: logInButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: desLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽100
        self.addConstraint(NSLayoutConstraint(item: logInButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高35
        self.addConstraint(NSLayoutConstraint(item: logInButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
    }
    
    func IconaddAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.removedOnCompletion = false
        //        animation.autoreverses = true
        iconView.layer.addAnimation(animation, forKey:nil)
    }
    
    //MARK: - 点击事件
    
    @objc private func registerBtnDidClick () {
        delegate?.registeClick()
    }
    
    @objc private func loginDidClick (btn:UIButton) {
        delegate?.loginClick()
    }
    
    //MARK: - 懒加载
    
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //添加遮罩层
    private lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"));
    
    private lazy var desLabel : UILabel = {
       let label = UILabel()
        
        label.text = "关注一些人，看看有什么惊喜"
        label.textColor = UIColor .darkGrayColor()
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var registerButton : UIButton = {
       let button = UIButton()
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button .setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor .orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "registerBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    private lazy var logInButton : UIButton = {
        let button = UIButton()
        
        button.setTitle("登录", forState: UIControlState.Normal)
        button .setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor .orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "loginDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
